/*
 * Copyright (C) 2008+ Spark Zheng
 *
 * src/test/test_conf.c
 * 
 * read a config file and the parse each item
 * conf testing see test_conf.c
 *
 * CONF_FILE example:
 * #this is a test config file of confloose
 * #
 * 
 * key4 = test key4
 * key3 = 456
 * key1 = value11, value12
 * key2 = value2
 * key0 = 234
 * 
 * #field1:
 * [field1]
 * key0 = 111
 * key1 = field1_value1
 * key2 = field1_value2
 * 
 * #field0:
 * [field0]
 * aaa = bbb
 * 
 * #field2:
 * [field2]
 * key0 = 100
 * key1 = field2_value1
 * key2 = field2_value2
 * 
 * key3 = field2_value31, field2_value32
 *
 */

#include <stdio.h>
#include <string.h>

#include "confloose.h"
#include "mempool.h"

#define CONFL_FILE	"./data/test.confl"

int main(int argc, char *argv[])
{
	int ret = 0;
	dlib_confl_t *confl = NULL;
	char *cfile = CONFL_FILE;
	char *str = NULL;
	long integer = 0;
	
	if (argc > 1)
		cfile = argv[1];
	
	confl = dlib_confl_init(cfile);
	if (confl == NULL) {
		fprintf(stderr, "CONFL: dlib_confl_init error, unexpected.\n");
		return -1;
	}
	
	ret = dlib_confl_get_integer(confl, "field1", "key0", &integer);
	if (ret == -E_CONFL_FIELD_NOTEXIST) {
		fprintf(stderr, "CONFL: field[field1] not exist.\n");
	} else if (ret == -E_CONFL_ITEM_NOTEXIST) {
		fprintf(stderr, "CONFL: field[field1] item[key0] not exist.\n");
	} else if (ret == 0) {
		fprintf(stdout, "CONFL: value of field[field1] item[key0]:"
			" %ld.\n", integer);
	}
	
	ret = dlib_confl_get_integer(confl, "fieldx", "key0", &integer);
	if (ret == -E_CONFL_FIELD_NOTEXIST) {
		fprintf(stderr, "CONFL: field[fieldx] not exist.\n");
	} else if (ret == -E_CONFL_ITEM_NOTEXIST) {
		fprintf(stderr, "CONFL: field[fieldx] item[key0] not exist.\n");
	} else if (ret == 0) {
		fprintf(stdout, "CONFL: value of field[fieldx] item[key0]:"
			" %ld.\n", integer);
	}
	
	ret = dlib_confl_get_integer(confl, "field1", "keyx", &integer);
	if (ret == -E_CONFL_FIELD_NOTEXIST) {
		fprintf(stderr, "CONFL: field[field1] not exist.\n");
	} else if (ret == -E_CONFL_ITEM_NOTEXIST) {
		fprintf(stderr, "CONFL: field[field1] item[keyx] not exist.\n");
	} else if (ret == 0) {
		fprintf(stdout, "CONFL: value of field[field1] item[keyx]:"
			" %ld.\n", integer);
	}
	
	ret = dlib_confl_get_integer(confl, NULL, "key3", &integer);
	if (ret == -E_CONFL_FIELD_NOTEXIST) {
		fprintf(stderr, "CONFL: field[NULL] not exist.\n");
	} else if (ret == -E_CONFL_ITEM_NOTEXIST) {
		fprintf(stderr, "CONFL: field[NULL] item[key3] not exist.\n");
	} else if (ret == 0) {
		fprintf(stdout, "CONFL: value of field[NULL] item[key3]:"
			" %ld.\n", integer);
	}
	
	fprintf(stdout, "-----------------------\n");
	
	str = dlib_confl_get_string(confl, NULL, "key3", CONFL_STR_COPY);
	if (str == NULL) {
		fprintf(stderr, "CONFL: field[NULL] item[key3] not exist.\n");
	} else {
		fprintf(stdout, "CONFL: value of field[NULL] item[key3]:"
			" %s.\n", str);
		dfree(str);
	}
	
	str = dlib_confl_get_string(confl, "field2", "key3", CONFL_STR_MOVE);
	if (str == NULL) {
		fprintf(stderr, "CONFL: field[field2] item[key3] not exist.\n");
	} else {
		fprintf(stdout, "CONFL: value of field[field2] item[key3]:"
			" %s.\n", str);
		dfree(str);
	}
	
	str = dlib_confl_get_string(confl, "fieldx", "key3", CONFL_STR_MOVE);
	if (str == NULL) {
		fprintf(stderr, "CONFL: field[fieldx] item[key3] not exist.\n");
	} else {
		fprintf(stdout, "CONFL: value of field[fieldx] item[key3]:"
			" %s.\n", str);
		dfree(str);
	}
	
	str = dlib_confl_get_string(confl, NULL, "key3", CONFL_STR_MOVE);
	if (str == NULL) {
		fprintf(stderr, "CONFL: field[NULL] item[key3] not exist.\n");
	} else {
		fprintf(stdout, "CONFL: value of field[NULL] item[key3]:"
			" %s.\n", str);
		dfree(str);
	}
	
	str = dlib_confl_get_string(confl, NULL, "key3", CONFL_STR_COPY);
	if (str == NULL) {
		fprintf(stderr, "CONFL: field[NULL] item[key3] not exist.\n");
	} else {
		fprintf(stdout, "CONFL: value of field[NULL] item[key3]:"
			" %s.\n", str);
		dfree(str);
	}
	
	dlib_confl_dump(confl);
	
	dlib_confl_exit(confl);

	return 0;
}
